﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading;
[assembly: log4net.Config.XmlConfigurator(ConfigFile = ".\\Log\\log4net.config", Watch = true)]
namespace RookieLibSharp.Log
{
    public enum LogErrorLevel
    {
        Debug = 0,
        Error,
        Fetal
    }
    public class LogHelper
    {
        public static void WriteLog(LogErrorLevel logErrorLevel,Type type, Exception ex)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(type);
            
            switch (logErrorLevel)
            {
                case LogErrorLevel.Debug:
                    log.Debug(logErrorLevel.ToString(), ex);
                    break;
                default:
                    break;
            }
        }
        public static void WriteLog(LogErrorLevel logErrorLevel, Type type, string msg)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(type);

            switch (logErrorLevel)
            {
                case LogErrorLevel.Debug:
                    log.Debug( msg);
                    break;
                default:
                    break;
            }
        }
        public static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            Exception ex = e.Exception as Exception;

            if (ex != null)
            {
                WriteLog(LogErrorLevel.Error, sender.GetType(), ex);
            }
            else
            {
                WriteLog(LogErrorLevel.Error, sender.GetType(), string.Format("Application_ThreadException:{0}", e));
            }
        }
        public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;   
            if (null != ex)
            {
                WriteLog(LogErrorLevel.Error,sender.GetType(),  ex);
            }
            else
            {
                WriteLog(LogErrorLevel.Error, sender.GetType(), string.Format("Application_UnhandledError:{0}", e));
            }
        }
    }
}
